package com.supor.aiot.basket.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.baseconfig.common.utils.GsonUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.supor.aiot.basket.R;
import com.supor.aiot.basket.api.BasketImpl;
import com.supor.aiot.basket.bean.IngredientBuyBean;
import com.supor.aiot.basket.bean.IngredientCommonBean;
import com.supor.aiot.basket.bean.RecipeBean;

import java.util.ArrayList;
import java.util.List;


public class IngredientDetailAdapter extends RecyclerView.Adapter<IngredientDetailAdapter.BasketHolder>{
    private List<IngredientCommonBean> mMainList;
    private OnIngredientChangeListener listener;
    private Context mContext;

    public IngredientDetailAdapter(List<IngredientCommonBean> list, Context mContext) {
        this.mContext = mContext;
        this.mMainList = list;
    }

    public void setListener(OnIngredientChangeListener listener){
        this.listener = listener;
    }

    @NonNull
    @Override
    public IngredientDetailAdapter.BasketHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_list_detail,parent, false);
        return new IngredientDetailAdapter.BasketHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BasketHolder holder, int position) {
        IngredientCommonBean ingredients = mMainList.get(position);
        holder.cbForAll.setChecked((ingredients.getIsBuy()==1?true:false));
        holder.cbForAll.setOnCheckedChangeListener((compoundButton, b) -> {
            changeIngredientState(ingredients.getAutoId()+"",b?1:0);
        });

        if (!TextUtils.isEmpty(ingredients.getIngredientsName()))holder.tvBasketTitle.setText(ingredients.getIngredientsName());
        if (!TextUtils.isEmpty(ingredients.getIngredientQuantity())&&!TextUtils.isEmpty(ingredients.getIngredientUnit()))holder.tvBasketNum.setText(ingredients.getIngredientQuantity()+ingredients.getIngredientUnit());
        if (!TextUtils.isEmpty(ingredients.getRecipes())){
            String recipes = ingredients.getRecipes().replaceAll("\\\\","");
            Gson gson = new Gson();
            JsonParser parser = new JsonParser();
            JsonArray Jarray = parser.parse(recipes).getAsJsonArray();
            ArrayList<RecipeBean> lcs = new ArrayList<RecipeBean>();
            for(JsonElement obj : Jarray ){
                RecipeBean cse = gson.fromJson( obj , RecipeBean.class);
                lcs.add(cse);
            }
            if (lcs.size()>1){
                String name ="";
                for (int i=0;i<lcs.size();i++){
                    if (i==0){
                        name+=lcs.get(i).getRecipeName();
                    }else {
                        name+=","+lcs.get(i).getRecipeName();
                    }
                }
                holder.tvDegree.setText(name);
            }else {
                holder.tvDegree.setText(lcs.get(0).getRecipeName());
            }
        }else {
            holder.tvDegree.setVisibility(View.GONE);
        }

        if (ingredients.getIsBuy()==1){
            holder.tvBasketTitle.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG); //中划线
            holder.tvBasketNum.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG); //中划线
            holder.tvDegree.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG); //中划线
        }
    }


    @SuppressLint("AutoDispose")
    private void changeIngredientState(String id, int state){
        IngredientBuyBean buyBean = new IngredientBuyBean();
        buyBean.setAutoId(id);
        buyBean.setIsBuy(state);

        BasketImpl.getInstance().changeIngredientBuy(buyBean).subscribe(result -> {
            if (result.isSuccess()) {
                if (listener!=null)listener.OnStateChanged();
            } else {
                Log.e("nail", "state error");
            }
        }, throwable -> {
            Log.e("nail", "state查询失败: " + throwable.getMessage());
        });
    }

    @Override
    public int getItemCount() {
        return mMainList.size();
    }

    public class BasketHolder extends RecyclerView.ViewHolder{
        private CheckBox cbForAll;
        private TextView tvBasketTitle;
        private TextView tvBasketNum;
        private TextView tvDegree;

        public BasketHolder(@NonNull View itemView) {
            super(itemView);
             cbForAll = itemView.findViewById(R.id.cb_for_all);
             tvBasketTitle = itemView.findViewById(R.id.tv_ingredient_title);
             tvBasketNum = itemView.findViewById(R.id.tv_ingredient_sum);
             tvDegree = itemView.findViewById(R.id.tv_basket_name);
        }
    }

    public interface OnIngredientChangeListener{
        void OnStateChanged();
    }

}
